'use strict';
const db=uniCloud.database();
// 分页查询
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const table = event.table
	const pageIndex = event.pageIndex || 1
	const pageSize = event.pageSize || 10
	const title = event.title || ''
	const filter = {
		title:new RegExp(title)
	}
	const countResult = await db.collection(table).where(filter).count()  //获取集合中的总记录数
	const total = countResult.total  //得到总记录数 
	// const totalPage = Math.ceil(total / pageSize)  //计算页数
	// if (pageIndex > totalPage || pageIndex == totalPage) {  //如果没有数据了，就返回false
	// 	hasMore = false 
	// } else {
	// 	hasMore = true 
  //  }
	// let result=await db.collection(table).limit(pageSize).where({}).get();
	let result = await db.collection(table).where(filter).skip((pageIndex - 1) * pageSize).limit(pageSize).get()
	console.log(result)  
	if(result.affectedDocs==0){
		return{
			code:500,
			msg:"没有数据"
		}
	}else{
		return{
			code:200,
			msg:"操作成功",
			total:total,
			data:result.data
		}
	}
	//返回数据给客户端
	return result
};
